package com.mobile.web.function;

import com.mobile.service.dto.FunctionDTO;
import com.mobile.service.dto.FunctionQueryDTO;
import com.mobile.service.inter.system.FunctionService;
import com.mobile.util.*;
import com.mobile.web.base.BaseController;
import com.mobile.web.config.util.Jurisdiction;
import com.mobile.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统功能菜单API接口
 */
@RestController
@RequestMapping("/function")
public class FunctionController extends BaseController {

    @Autowired
    FunctionService functionService;

    /**
     * 获取系统所有菜单及下面子菜单
     *
     * @return
     */
    @GetMapping("/getMenu")
    public Object getMenu() {
        Map<String, Object> map = new HashMap<>();
        try {
            List<PageData> allmenuList = functionService.listAllMenuQx("0");
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
            map.put("menuList", allmenuList);
        } catch (Exception e) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }
    /**
     * 菜单页左列表展示
     *
     * @return
     */
    @RequestMapping(value = "/leftMenuList")
    //@RequiresPermissions("function:read:1")
    public Object leftMenuList() {
        Map<String, Object> map = new HashMap<>();
        try {
            List<PageData> allmenuList = functionService.listAllMenu("0");
            PageData menuSlots;
            //给菜单加图标
            for (PageData pageData : allmenuList) {
                menuSlots = new PageData();
                menuSlots.put("icon", "profile");
                pageData.put("slots", menuSlots);
                List<PageData> childrens = (List<PageData>)pageData.get("subMenu");
                for (PageData children : childrens) {
                    menuSlots = new PageData();
                    menuSlots.put("icon", "file-text");
                    children.put("slots", menuSlots);
                }
            }
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
            map.put("menuList", allmenuList);
        } catch (Exception e) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    /**
     * 菜单列表(分页)
     *
     * @return
     */
    @PostMapping(value = "/getFunctionListPage")
    //@RequiresPermissions("function:read:1")
    public Object getFunctionListPage(FunctionQueryDTO functionQueryDTO) {
        Map<String, Object> map = new HashMap<>();
        try {
            PageResult<FunctionDTO> functionPage = functionService.getMenuList(functionQueryDTO);
            List<FunctionDTO> functionList = functionPage.getPageDatas();
            List<FunctionDTO> parentList = new ArrayList<>();
            //拼接菜单树形数据
            for (FunctionDTO functionDTO : functionList) {
                //获取最上级菜单
                if ("0".equals(functionDTO.getParentId().toString())) {
                    parentList.add(functionDTO);
//                    functionList.remove(pageData);
                }
            }
            List<FunctionDTO> list;
            for (FunctionDTO data : parentList) {
                list = new ArrayList<>();
                for (FunctionDTO funDto : functionList) {
                    if (funDto.getParentId().equals(data.getFunctionId())) {
                        list.add(funDto);
//                        functionList.remove(pageData);
                    }
                }
                data.setChildren(list);
            }
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
            map.put("data", parentList);
            map.put("QX", Jurisdiction.getHC(this.getUserCode(),"function"));
        } catch (Exception e) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }
    /**
     * 添加功能菜单
     * @return
     */
    @PostMapping(value = "/addFunction")
    //@RequiresPermissions("function:add:1")
    public Object addDict() {
        Map<String, Object> map;
        PageData pd = this.getPageData();

        try {
            map = functionService.insertFunction(pd);
        } catch (Exception e) {
            map = new HashMap<>();
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        removeMenuList();
        return AppUtil.returnObject(pd, map);
    }
    /**
     * 修改功能菜单
     * @return
     */
    @PostMapping(value = "/editFunction")
    //@RequiresPermissions("function:edit:1")
    public Object editFunction() {
        Map<String, Object> map;
        PageData pd = this.getPageData();
        try {
            map = functionService.updateFunction(pd);
        } catch (Exception e) {
            map = new HashMap<>();
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        removeMenuList();
        return AppUtil.returnObject(pd, map);
    }
    /**
     * 根据ID查询功能菜单
     * @return
     */
    @PostMapping(value = "/findFunctionById")
    //@RequiresPermissions("function:read:1")
    public Object findFunctionById() {
        Map<String, Object> map = new HashMap<>();
        PageData pd = this.getPageData();

        try {
            PageData funcItems = functionService.findFunctionById(pd.getString("functionId"));
            map.put("data", funcItems);
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
            if (StringUtil.isEmpty(funcItems)) {
                map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
                map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0001"));
            }
        } catch (Exception e) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }

        return AppUtil.returnObject(pd, map);
    }


    /**
     * 删除菜单
     * @return
     */
    @PostMapping(value = "/removeFunc")
    //@RequiresPermissions("function:delete:1")
    public Object removeFunc() {
        Map<String, Object> map = new HashMap<>();
        PageData pd = this.getPageData();
        try {
            map=functionService.removeFunc(pd);
        } catch (Exception e) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        removeMenuList();
        return AppUtil.returnObject(pd, map);
    }
    /**
     * 改变功能菜单有效值
     */
    @PostMapping(value = "/editEffective")
    //@RequiresPermissions("function:edit:1")
    public Object editEffective() {
        Map<String, Object> map;
        PageData pd = this.getPageData();
        try {
            map = functionService.editEffective(pd);
        } catch (Exception e) {
            map = new HashMap<>();
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        removeMenuList();
        return AppUtil.returnObject(pd, map);
    }



    /**
     * 移除session数据中的菜单列表
     */
    public void removeMenuList() {
        Jedis jedis = RedisUtil.getJedis();
        try {
            //移除菜单数据,重新加载
            jedis.hdel( Const.SESSION_allmenuList,this.getUserCode());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        } finally {
            jedis.close();
        }
    }

}
